package LeetCode刷题;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-09-25 20:38
 * @description: Functions of this class is
 * 官方解法：
 * class Solution {
 *     public List<List<Integer>> levelOrder(TreeNode root) {
 *        //队列实现
 *         List<List<Integer>> ans = new ArrayList();
 *         if (root != null) {
 *             LinkedList<TreeNode> q = new LinkedList();//声明一个队列存储每一层的节点
 *             q.add(root);
 *             while(!q.isEmpty()){
 *                  List<Integer> sub = new ArrayList();//声明一个集合存储每一层的所有节点的值
 *                  int level=q.size();//**将队列中结点的数量作为层数
 *                  while(level-->0){
 *                      TreeNode p=q.pop();
 *                      sub.add(p.val);
 *                      if(p.left!=null){
 *                          q.add(p.left);
 *                      }
 *                      if(p.right!=null){
 *                          q.add(p.right);
 *                      }
 *                  }
 *                  ans.add(sub);
 *             }
 *         }
 *          return ans;
 *     }
 * }
 **/
public class 二叉树层序遍历 {
    public static void main(String args[]) {

    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static List<List<Integer>> levelOrder(TreeNode root) {
        //队列实现
        List<List<Integer>> ans = new ArrayList();
        if (root != null) {
            List<Integer> sub = new ArrayList();
            LinkedList<TreeNode> q = new LinkedList();
            q.add(root);
            sub.add(root.val);
            ans.add(sub);

        }
        return ans;
    }
}